<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/icon.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/ficon.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/icon.png">
  <link rel="mask-icon" href="/images/icon.png" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"bqlin.github.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":true,"comments":{"style":"tabs","active":"gitalk","storage":true,"lazyload":false,"nav":{"gitalk":{"order":-2}},"activeClass":"gitalk"},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":-1,"unescape":false,"preload":false},"motion":{"enable":false,"async":true,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="OC的Runtime机制使得它可以被认为是一种动态语言。 Swift则取消了Runtime这个能力，让Swift称为一门静态语言。Swift语言的对象方法调用基本在编译期间就被确定，可以看做是一种硬编码形式的调用实现。这种机制加快了程序的运行速度、减少程序包体积，但在编译连接优化功能开启时反而又会出现增大包提及的情况。Swift在编译连接期间采用的是空间换时间的优化策略，以提高运算行速度为主要优化">
<meta property="og:type" content="article">
<meta property="og:title" content="Swift 底层">
<meta property="og:url" content="https://bqlin.github.io/posts/swift_underlying/index.html">
<meta property="og:site_name" content="權咚领域">
<meta property="og:description" content="OC的Runtime机制使得它可以被认为是一种动态语言。 Swift则取消了Runtime这个能力，让Swift称为一门静态语言。Swift语言的对象方法调用基本在编译期间就被确定，可以看做是一种硬编码形式的调用实现。这种机制加快了程序的运行速度、减少程序包体积，但在编译连接优化功能开启时反而又会出现增大包提及的情况。Swift在编译连接期间采用的是空间换时间的优化策略，以提高运算行速度为主要优化">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/bqlin/image-land/raw/master/3qML8t.jpg">
<meta property="article:published_time" content="2021-11-15T09:12:17.000Z">
<meta property="article:modified_time" content="2021-11-15T09:12:17.000Z">
<meta property="article:author" content="權咚">
<meta property="article:tag" content="Apple">
<meta property="article:tag" content="Swift">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/bqlin/image-land/raw/master/3qML8t.jpg">

<link rel="canonical" href="https://bqlin.github.io/posts/swift_underlying/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Swift 底层 | 權咚领域</title>
  
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-105338942-1"></script>
    <script>
      if (CONFIG.hostname === location.hostname) {
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());
        gtag('config', 'UA-105338942-1');
      }
    </script>


  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?98fcf61fd269408d07dcbc1e49311f9a";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="權咚领域" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">權咚领域</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Making app a work of art.</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://bqlin.github.io/posts/swift_underlying/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/icon.png">
      <meta itemprop="name" content="權咚">
      <meta itemprop="description" content="🤘🤯👾">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="權咚领域">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Swift 底层
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-11-15 17:12:17" itemprop="dateCreated datePublished" datetime="2021-11-15T17:12:17+08:00">2021-11-15</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/iOS/" itemprop="url" rel="index"><span itemprop="name">iOS</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>3k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>3 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>OC的Runtime机制使得它可以被认为是一种动态语言。</p>
<p>Swift则取消了Runtime这个能力，让Swift称为一门静态语言。Swift语言的对象方法调用基本在编译期间就被确定，可以看做是一种硬编码形式的调用实现。这种机制加快了程序的运行速度、减少程序包体积，但在编译连接优化功能开启时反而又会出现增大包提及的情况。Swift在编译连接期间采用的是空间换时间的优化策略，以提高运算行速度为主要优化考虑点。</p>
<h2 id="oc类对象方法调用">OC类对象方法调用</h2>
<p>OC对象调用方法都通过消息发送的<code>objc_msgSend</code>完成，并至少传入调用对象和对象方法名称作为参数，方法根据对象找到类结构信息，通过方法名来找到最终调用的方法函数地址，并最终完成函数的调用。这是OC Runtime的实现机制，同时也是OC对多态的实现。</p>
<h2 id="swift类对象">Swift类对象</h2>
<p>Swift类对象按其基类可分为：</p>
<ul>
<li>从NSObject及其子类派生的类。</li>
<li>SwiftObject派生的类（SwiftObject是隐藏的类，不会在源代码中体现）。</li>
</ul>
<p>Swift类对象内存布局和OC类内存布局相似。都是在最开始部分有一个isa指针，指向类的描述信息。Swift类的描述信息结构继承自OC类的描述信息，但没有完全使用其中的属性，对于方法的调用主要是使用其中扩展的序函数表的区域。</p>
<p>Swift对象实例都是在堆内存中创建，与OC一致。Swift类在实例化时，会生成堆内存分配和初始化函数，形式为：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">模块名.类名.__allocating_init(类名,初始化参数)</span><br></pre></td></tr></table></figure>
<p>与OC一致，Swift类实例也是通过引用计数管理生命周期，所以也会在编译时插入<code>swift_retain</code>、<code>swift_release</code>函数，当前引用计数为0后，就调用生成的西沟和销毁函数，形式为：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">模块名.类名.__deallocating_deinit(对象)</span><br></pre></td></tr></table></figure>
<h2 id="swift方法调用">Swift方法调用</h2>
<p>Swift类定义的方法可分为：</p>
<ul>
<li>OC类派生类并重写的方法</li>
<li>扩展中定义的方法</li>
<li>类中定义的一般方法</li>
</ul>
<p>对于这三种方法，系统采用的处理和调用机制是完全不一样的。</p>
<h3 id="oc类派生类并重写的方法">OC类派生类并重写的方法</h3>
<p>这些方法还是与OC类原本方法的调用机制一致，即也是使用<code>objc_msgSend</code>调用。</p>
<h3 id="扩展中定义的方法">扩展中定义的方法</h3>
<p>这里的扩展中定义的方法是不包含重写OC基类的方法。这种方法调用时在编译期就决定的，即在调用方法时，直接使用硬编码的函数地址。这也决定了扩展中的方法无法在运行时做替换和改变。</p>
<p>同时这类方法的符号信息不保存到类的描述信息中，也决定了派生类中不能重写扩展中的定义的方法，即不支持多态。</p>
<h3 id="类中定义的一般方法">类中定义的一般方法</h3>
<p>Swift在未开启编译链接优化时，对象的方法调用实现机制和C++的虚函数调用机制类似。Swift为每个类都建立一个虚函数表的数组结构，保存着所以定义的常规成员方法的地址。</p>
<p>在方法调用时，不再想使用<code>objc_msgSend</code>调用传入调用对象和方法名，而是直接调用函数地址，而调用对象则存在x20寄存器中，让代码更加安全。</p>
<p>虽然类中方法在虚函数表中的索引值是在编译期确定的，但基类和派生类虚函数表中相同索引处的函数地址可以不一样，即当子类重写了父类某个方法时，会分别生成两个类的虚函数表，在相同的索引位置保存不同的函数地址实现多态。</p>
<p>另外，为了实现方法重载和运算符重载，函数名字会进行修饰重命名，规则如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">_$s&lt;模块名长度&gt;&lt;模块名&gt;&lt;类名长度&gt;&lt;类名&gt;C&lt;方法名长度&gt;&lt;方法名&gt;yy&lt;参数类型<span class="number">1</span>&gt;_&lt;参数类型<span class="number">2</span>&gt;_&lt;参数类型N&gt;F</span><br></pre></td></tr></table></figure>
<h2 id="swift的成员变量">Swift的成员变量</h2>
<p>OC类的成员变量根据定义顺序排在isa后面，另外还会生成一张变量偏移表，通过偏移来访问成员变量。</p>
<p>Swift简化了对成员变量的访问。直接在编译链接时确定成员变量在对象的偏移位置。且不生成变量编译信息表。</p>
<h2 id="结构体">结构体</h2>
<p>初始化器与属性默认值在汇编上是一样的。</p>
<p>Swift结构体与C结构体的内存结构是一样的，成员变量内存都是紧挨在一起。</p>
<p>Swift结构体内存结构中没有保存isa，即没有保存结构体信息，因此不支持多态与派生，同时结构体中的所有方法都是通过在编译期硬编码实现的。</p>
<h2 id="类方法和全局函数">类方法和全局函数</h2>
<p>Swift类方法和全局函数不存在对象作为参数，即不需要把对象保存到x20寄存器中，所以它就是个简单的C语言普通函数，只是方法名需要进行修饰重命名，所有对类方法和全局函数的调用都是在编译期硬编码为函数地址来调用的。</p>
<h2 id="开启编译链接优化后">开启编译链接优化后</h2>
<p>开启编译链接优化后，Swift对象方法的调用机制做了很大的优化，最主要是弱化了通过虚函数表来间接调用方法的实现，而是大量改用一些内联的方式来处理方法函数的调用。</p>
<p>对于多态的支持，可能不是通过虚函数来处理，而是通过类型判断，然后用条件语句分支执行方法。</p>
<h2 id="swift其他特性底层">Swift其他特性底层</h2>
<h3 id="引用类型与值类型">引用类型与值类型</h3>
<p>值类型存在栈中，引用类型存在堆中。若值类型中包含引用类型，则存储的只是引用类型的指针。</p>
<h3 id="延迟存储属性">延迟存储属性</h3>
<p>多线程同时第一次访问lazy属性，是无法保证属性只被初始化一次。</p>
<p>当结构体包含一个延迟存储属性时，只有<code>var</code>才能访问延迟存储属性。因为延迟属性初始化时要改变结构体的内存，而let修饰的结构体要求里面的所以成员都完成初始化，两者相悖。</p>
<p>延迟属性也不能添加属性观察器。</p>
<h3 id="类存储属性">类存储属性</h3>
<p>类存储属性默认就是<code>lazy</code>修饰，且能有<code>let</code>修饰成常量，会在第一次使用的时候才初始化。类存储属性时多线程安全的（系统底层会有加锁处理）。这其中调用了<code>swift_once</code>函数，内部调用了<code>dispatch_once</code>函数。</p>
<p>类存储属性实际上是全局变量。</p>
<h3 id="输入输出参数">输入输出参数</h3>
<p>inout参数本质是地址传递（引用传递）。在汇编使用<code>leaq</code>传递地址，而一般参数则是使用<code>movq</code>传递值。</p>
<p>限制：</p>
<ul>
<li>不能有默认值</li>
<li>需要时左值。</li>
</ul>
<p>对于有属性观察器以及计算属性，在get/set输入输出参数时，也会触发相关的方法调用。</p>
<p>所以，如果实参有物理内存地址，且有设置属性观察器，则直接把实参的内存地址传入参数（实参进行引用传递）。</p>
<p>如果实参是计算属性或设置了属性观察器，则采取Copy In Copy Out的做法：在调用该函数时，先复制实参的值，产生副本（可以理解成getter操作），将副本的内存地址传入参数（副本进行引用传递），在函数内部修改副本的值，函数返回后，再将副本的值覆盖实参的值（可以理解为setter操作）。</p>
<h3 id="函数重载">函数重载</h3>
<p>返回值不参与函数重载。</p>
<h3 id="内联函数">内联函数</h3>
<p>开启编译器优化后，编译器会将某些函数转为内联函数，即把函数展开成函数体。但以下情况不会被自动内联：</p>
<ul>
<li>函数体较长</li>
<li>包含递归调用的函数</li>
<li>包含动态派发的函数</li>
</ul>
<h3 id="闭包">闭包</h3>
<p>闭包的定义：一个函数和它所捕获的变量/常量环境组合起来。</p>
<p>闭包函数会把捕获的变量存储到堆中动态申请的内存空间上。存储的信号包含类型描述信息、引用计数信息、具体值。每次闭包函数调用，都会动态申请一段新的堆内存在存放新的捕获变量。若没有捕获变量，则不回分配堆空间。</p>
<p>内存布局如下：</p>
<p><img src="https://gitee.com/bqlin/image-land/raw/master/3qML8t.jpg" /></p>
<p>所以闭包的内存布局与实例对象很相似：共享方法、各自管理自己的成员变量。</p>
<p>空合并运算符<code>??</code>默认值是个自动闭包，这可以在可以解包的情况下不调用默认值的逻辑。</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">func</span> <span class="title function_">??</span> &lt;<span class="type">T</span>&gt;(<span class="params">optional</span>: <span class="type">T</span>?, <span class="params">defaultValue</span>: <span class="keyword">@autoclosure</span> () <span class="keyword">throws</span> -&gt; <span class="type">T</span>?) <span class="keyword">rethrows</span> -&gt; <span class="type">T</span>?</span><br></pre></td></tr></table></figure>
<h2 id="参考">参考</h2>
<ul>
<li><a target="_blank" rel="noopener" href="https://juejin.cn/post/6966857669508825096">Swift底层原理探索5----闭包 - 掘金</a></li>
<li><a target="_blank" rel="noopener" href="https://juejin.cn/post/6844903889523884039">Swift5.0 的 Runtime 机制浅析 - 掘金</a></li>
</ul>

    </div>

    
    
    
        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Apple/" rel="tag"># Apple</a>
              <a href="/tags/Swift/" rel="tag"># Swift</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/posts/runtime_weak/" rel="prev" title="Runtime：weak">
      <i class="fa fa-chevron-left"></i> Runtime：weak
    </a></div>
      <div class="post-nav-item">
    <a href="/posts/block/" rel="next" title="Objective-C：Block">
      Objective-C：Block <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="gitalk-container"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#oc%E7%B1%BB%E5%AF%B9%E8%B1%A1%E6%96%B9%E6%B3%95%E8%B0%83%E7%94%A8"><span class="nav-text">OC类对象方法调用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#swift%E7%B1%BB%E5%AF%B9%E8%B1%A1"><span class="nav-text">Swift类对象</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#swift%E6%96%B9%E6%B3%95%E8%B0%83%E7%94%A8"><span class="nav-text">Swift方法调用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#oc%E7%B1%BB%E6%B4%BE%E7%94%9F%E7%B1%BB%E5%B9%B6%E9%87%8D%E5%86%99%E7%9A%84%E6%96%B9%E6%B3%95"><span class="nav-text">OC类派生类并重写的方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%89%A9%E5%B1%95%E4%B8%AD%E5%AE%9A%E4%B9%89%E7%9A%84%E6%96%B9%E6%B3%95"><span class="nav-text">扩展中定义的方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%B1%BB%E4%B8%AD%E5%AE%9A%E4%B9%89%E7%9A%84%E4%B8%80%E8%88%AC%E6%96%B9%E6%B3%95"><span class="nav-text">类中定义的一般方法</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#swift%E7%9A%84%E6%88%90%E5%91%98%E5%8F%98%E9%87%8F"><span class="nav-text">Swift的成员变量</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%93%E6%9E%84%E4%BD%93"><span class="nav-text">结构体</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%B1%BB%E6%96%B9%E6%B3%95%E5%92%8C%E5%85%A8%E5%B1%80%E5%87%BD%E6%95%B0"><span class="nav-text">类方法和全局函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%BC%80%E5%90%AF%E7%BC%96%E8%AF%91%E9%93%BE%E6%8E%A5%E4%BC%98%E5%8C%96%E5%90%8E"><span class="nav-text">开启编译链接优化后</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#swift%E5%85%B6%E4%BB%96%E7%89%B9%E6%80%A7%E5%BA%95%E5%B1%82"><span class="nav-text">Swift其他特性底层</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BC%95%E7%94%A8%E7%B1%BB%E5%9E%8B%E4%B8%8E%E5%80%BC%E7%B1%BB%E5%9E%8B"><span class="nav-text">引用类型与值类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BB%B6%E8%BF%9F%E5%AD%98%E5%82%A8%E5%B1%9E%E6%80%A7"><span class="nav-text">延迟存储属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%B1%BB%E5%AD%98%E5%82%A8%E5%B1%9E%E6%80%A7"><span class="nav-text">类存储属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E5%8F%82%E6%95%B0"><span class="nav-text">输入输出参数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%87%BD%E6%95%B0%E9%87%8D%E8%BD%BD"><span class="nav-text">函数重载</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%86%85%E8%81%94%E5%87%BD%E6%95%B0"><span class="nav-text">内联函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%97%AD%E5%8C%85"><span class="nav-text">闭包</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%82%E8%80%83"><span class="nav-text">参考</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="權咚"
      src="/images/icon.png">
  <p class="site-author-name" itemprop="name">權咚</p>
  <div class="site-description" itemprop="description">🤘🤯👾</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">84</span>
          <span class="site-state-item-name">文章</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">10</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/bqlin" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;bqlin" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://weibo.com/hearingdog" title="Weibo → https:&#x2F;&#x2F;weibo.com&#x2F;hearingdog" rel="noopener" target="_blank"><i class="fab fa-weibo fa-fw"></i>Weibo</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://xiaozhuanlan.com/metal-reference-translation" title="https:&#x2F;&#x2F;xiaozhuanlan.com&#x2F;metal-reference-translation" rel="noopener" target="_blank">Metal内参</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://xiaozhuanlan.com/wwdc21" title="https:&#x2F;&#x2F;xiaozhuanlan.com&#x2F;wwdc21" rel="noopener" target="_blank">WWDC21 内参</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2016 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">權咚</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">279k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">4:14</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/pangu@4/dist/browser/pangu.min.js"></script>

<script src="/js/utils.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  
  <script>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>











<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'default',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  

  

<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.css">

<script>
NexT.utils.loadComments(document.querySelector('#gitalk-container'), () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js', () => {
    var gitalk = new Gitalk({
      clientID    : '7ed324dbfd2741c35c0e',
      clientSecret: '75a59e1f6a55202c4a1b0ec3570ad5b5dccbc390',
      repo        : 'bqlin.github.io',
      owner       : 'bqlin',
      admin       : ['bqlin'],
      id          : 'bc26d3b4d405ebfd3da7d4b0dd3776ae',
        language: 'zh-CN',
      distractionFreeMode: true
    });
    gitalk.render('gitalk-container');
  }, window.Gitalk);
});
</script>

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"scale":1,"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","vOffset":-100},"mobile":{"show":false,"scale":0.5},"log":false});</script></body>
</html>
